﻿Public Class AppearenceResult

    Private sngWidth As Single = 2

    Private lblInput As TenKeyLabel

    Private fIsOnLabel As Boolean = False

    Event InputCompleted(ByVal sender As Object, ByVal e As EventArgs)

    Public Property Value1 As Integer
        Get
            If IsNumeric(tkl1.Text) Then
                Return CInt(tkl1.Text)
            Else
                Return 0
            End If
        End Get
        Set(ByVal value As Integer)
            tkl1.Text = value.ToString
        End Set
    End Property

    Public Property Value2 As Integer
        Get
            If IsNumeric(tkl2.Text) Then
                Return CInt(tkl2.Text)
            Else
                Return 0
            End If
        End Get
        Set(ByVal value As Integer)
            tkl1.Text = value.ToString
        End Set
    End Property

    Public Overrides Property Text As String
        Get
            Return tkl1.Text & Label1.Text & tkl2.Text & Label2.Text
        End Get
        Set(ByVal value As String)
            If String.IsNullOrEmpty(value) Then
                tkl1.Text = ""
                tkl2.Text = ""
            Else
                Dim valArr() As String = value.Replace(Label2.Text, "").Split(CChar(Label1.Text))
                If valArr.Length = 2 Then
                    If IsNumeric(valArr(0)) AndAlso IsNumeric(valArr(1)) Then
                        tkl1.Text = valArr(0)
                        tkl2.Text = valArr(1)
                    End If
                End If
            End If

        End Set
    End Property

    Public Property InputWidth As Single
        Get
            Return sngWidth
        End Get
        Set(ByVal value As Single)
            sngWidth = value
            tkl2.Width = Me.Font.Size * 2.5 * sngWidth
            tkl1.Width = Me.Font.Size * 2.5 * sngWidth
            AppearenceResult_SizeChanged(Me, New EventArgs)
        End Set
    End Property

    Public Property InputLabel As TenKeyLabel
        Get
            Return lblInput
        End Get
        Set(ByVal value As TenKeyLabel)

        End Set
    End Property

    Private Sub AppearenceResult_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Clear()
    End Sub

    Private Sub AppearenceResult_FontChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.FontChanged
        Label1.Height = Me.Font.Size * 2.5
        Label1.Width = Me.Font.Size * 1.5
        Label2.Height = Me.Font.Size * 2.5
        Label2.Width = Me.Font.Size * 1.5
        tkl1.Height = Me.Font.Size * 2.5
        tkl2.Height = Me.Font.Size * 2.5

        AppearenceResult_SizeChanged(Me, New EventArgs)
    End Sub

    Private Sub AppearenceResult_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged

        If Me.Height < Label1.Height Then
            Me.Height = Label1.Height
            Label1.Top = 0
            Label2.Top = 0
            tkl1.Top = 0
            tkl2.Top = 0
        Else
            Label1.Top = (Me.Height - Label1.Height) / 2
            Label2.Top = (Me.Height - Label2.Height) / 2
            tkl1.Top = (Me.Height - tkl1.Height) / 2
            tkl2.Top = (Me.Height - tkl2.Height) / 2
        End If

        If Me.Width < Label1.Width + Label2.Width + tkl1.Width + tkl2.Width Then
            Me.Width = Label1.Width + Label2.Width + tkl1.Width + tkl2.Width
            tkl1.Left = 0
            Label1.Left = tkl1.Right
            tkl2.Left = Label1.Right
            Label2.Left = tkl2.Right
        Else
            Label2.Left = Me.Width - Label2.Width
            Label1.Left = (Label2.Left - Label1.Width) / 2
            tkl1.Left = (Label1.Left - tkl1.Width) / 2
            tkl2.Left = Label1.Right + tkl1.Left
        End If

    End Sub

    Public Sub Clear()
        tkl1.Text = String.Empty
        tkl2.Text = String.Empty
    End Sub

    Protected Overrides Sub OnClick(ByVal e As System.EventArgs)
        If Not fIsOnLabel Then
            Exit Sub
        End If
        MyBase.OnClick(e)
    End Sub

    Private Sub tkl1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles tkl1.Click
        lblInput = tkl1
        fIsOnLabel = True
        Me.OnClick(e)
    End Sub

    Private Sub tkl2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles tkl2.Click
        lblInput = tkl2
        fIsOnLabel = True
        Me.OnClick(e)
    End Sub

    Private Sub tkl1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles tkl1.MouseLeave
        fIsOnLabel = False
    End Sub

    Private Sub tklTemp2_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles tkl2.MouseLeave
        fIsOnLabel = False
    End Sub

    Private Sub tkl_EndInput(ByVal sender As Object) Handles tkl1.InputCompleted, tkl2.InputCompleted
        'If IsNumeric(tkl1.Text) AndAlso IsNumeric(tkl2.Text) Then
        On_InputCompleted()
        'End If
    End Sub

    Overridable Sub On_InputCompleted()
        RaiseEvent InputCompleted(Me, New EventArgs)
    End Sub
End Class
